package org.catspaw.cherubim.text.stroke;

import java.io.Serializable;
import java.util.Comparator;

public class StrokeComparator implements Comparator<String>, Serializable {

	private static final long	serialVersionUID	= -5489420461893631191L;
	private StrokeDictionary	dictionary;

	public StrokeComparator(StrokeDictionary dictionary) {
		this.dictionary = dictionary;
	}

	public int compare(String o1, String o2) {
		for (int i = 0; i < o1.length() && i < o2.length(); i++) {
			int codePoint1 = o1.codePointAt(i);
			int codePoint2 = o2.codePointAt(i);
			if (codePoint1 == codePoint2) {
				continue;
			}
			int stroke1 = dictionary.lookupStroke(codePoint1);
			int stroke2 = dictionary.lookupStroke(codePoint2);
			if (stroke1 < 0 || stroke2 < 0) {
				return codePoint1 - codePoint2;
			}
			if (stroke1 != stroke2) {
				return stroke1 - stroke2;
			}
		}
		return o1.length() - o2.length();
	}
}
